function Url() {
};

Url.update_query = function(query) {
  if (parent != window) {
    if (parent.window.history.pushState) parent.window.history.pushState("object or string", "Title", '?' + query);
  } else {
    if (window.history.pushState) window.history.pushState("object or string", "Title", '?' + query);
  }
};

Url.insert_param = function(key, value) {
  key = encodeURI(key);
  value = encodeURI(value);
  if (parent != window) {
    var kvp = parent.document.location.search.substr(1).split('&');
  } else {
    var kvp = document.location.search.substr(1).split('&');
  }

  var i = kvp.length;
  var x;
  while (i--) {
    x = kvp[i].split('=');
    if (x[0] == key) {
      x[1] = value;
      kvp[i] = x.join('=');
      break;
    }
  }
  if (i < 0) {
    kvp[kvp.length] = [ key, value ].join('=');
  }
  //Chrome, Safari, FF4+, and IE10pp4+
  if (kvp.length > 0 && kvp[0] == '') kvp.splice(0, 1);
  if (parent != window) {
    if (parent.window.history.pushState) parent.window.history.pushState("object or string", "Title", '?' + kvp.join('&'));
  } else {
    if (window.history.pushState) window.history.pushState("object or string", "Title", '?' + kvp.join('&'));
  }
};

Url.get_param = function(name, def) {
  if (parent != window) {
    return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(parent.location.search) || [ , def ])[1]);
  } else {
    return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [ , def ])[1]);
  }
};

Url.redirect = function(md, url) {
  if (md == 1) {
    $('.ui-dialog').remove();
    $('#noty_inline_layout_container').remove();
    
    var parsed_url = new parseUri(url);
    sys = (parsed_url.host) ? parsed_url.host.substr(0, parsed_url.host.indexOf('.')) : 'index';
    Url.update_query(parsed_url.query);
    
    Ajax.request(
      '__pg.php',
      '__sys=' + sys + ((parsed_url.query && parsed_url.query !='') ? '&' + parsed_url.query : ''),
      function(rslt) {
        if (parent != window) {
          if (typeof parent.____func == 'undefined') {
            parent.____update(parsed_url, rslt, parent.$('.cs_body'));
          } else {
            parent.____func(parsed_url, rslt, parent.$('.cs_body'));
          }
        } else {
          if (typeof ____func == 'undefined') {
            ____update(parsed_url, rslt, $('.cs_body'));
          } else {
            ____func(parsed_url, rslt, $('.cs_body'));
          }
        }
        window.scrollTo(0, 0);
      },
      { noobj: 1 }
    );
  } else if (md == 0) {
    if (parent != window) {
      parent.location.href = url;
    } else {
      location.href = url;
    }
  }
  return false;
};